/*
 * WiichuckJoyServo -- Use a Wii Nunchuck joystick to control a servo
 *
 *
 * Tod E. Kurt, http://todbot.com/blog/
 *
 */
 
#include <Servo.h>

#include <Wire.h>
#include "nunchuck_funcs.h"

Servo servoX;
Servo servoY;

int ledPin    = 13;
int servoYPin = 10;
int servoXPin = 9;

byte joyx,joyy,zbut,cbut;

int loop_cnt;

void setup()
{
    servoX.attach( servoXPin );
    servoX.attach( servoYPin );

    Serial.begin(19200);
    nunchuck_setpowerpins();
    nunchuck_init(); // send the initilization handshake
    
    Serial.print("WiichuckJoyServo ready\n");
}

void loop()
{
    if( loop_cnt > 100 ) { // every 100 msecs get new data
        loop_cnt = 0;

        nunchuck_get_data();

        joyx = nunchuck_joyx();
        joyy = nunchuck_joyy();
        zbut = nunchuck_zbutton();
        cbut = nunchuck_cbutton(); 
            
        servoX.write( joyx );
        servoY.write( joyy );

        Serial.print("joyx: ");   Serial.print((byte)joyx,DEC);
        Serial.print("\tjoyy: "); Serial.print((byte)joyy,DEC);
        Serial.print("\tzbut: "); Serial.print((byte)zbut,DEC);
        Serial.print("\tcbut: "); Serial.println((byte)cbut,DEC);

    }
    loop_cnt++;
    delay(1);
}



